import backtrader as bt
import datetime
import pandas as pd
from strategies.strategy_001 import StrategyClass

params = dict(
    fromdate=datetime.datetime(2023, 11, 8),  # 回测开始时间
    todate=datetime.datetime(2023, 11, 15),  # 回测结束时间
    timeframe=bt.TimeFrame.Minutes,
    compression=1,
    dtformat=('%Y-%m-%d %H:%M:%S'),  # 日期和时间格式
    tmformat=('%H:%M:%S')# 时间格式
    )

# 数据地址
data_path = "rb2401.csv"
# 读取数据，并整理数据，使得index是datetime格式，index对应的是参数里面的0
dataframe = pd.read_csv(data_path)
dataframe['Datatime'] = pd.to_datetime(dataframe['date'])
dataframe.set_index('Datatime', inplace=True)
# 添加数据到cerebro中
feed = bt.feeds.PandasDirectData(dataname=dataframe, **params)

cerebro = bt.Cerebro()
cerebro.adddata(feed, name="FU2401.SHFE")

cerebro.addstrategy(StrategyClass, half=1000)

print('启动资金：%.2f' % cerebro.broker.getvalue())
cerebro.run(runonce=False)
print('期末资金：%.2f' % cerebro.broker.getvalue())
cerebro.plot(volume=False)[0][0].set_size_inches(9.6, 7)
